#!/usr/bin/env python

#==============================================================================
# Functionality
#==============================================================================
import pdb
import sys
import os
import re

# utility funcs, classes, etc go here.

def asserting(cond):
    if not cond:
        pdb.set_trace()
    assert(cond)

def has_stdin():
    return not sys.stdin.isatty()

def reg(pat, flags=0):
    return re.compile(pat, re.VERBOSE | flags)

#==============================================================================
# Cmdline
#==============================================================================
import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
    description="""
TODO
""")
     
parser.add_argument('-v', '--verbose',
    action="store_true",
    help="verbose output" )

args = None

#==============================================================================
# Main
#==============================================================================

def run():
    if args.verbose:
        print args
    if len(args.args) <= 0 and not has_stdin():
        # if there were no args and there was no input, prompt user.
        print 'Enter input (press Ctrl-D when done):'
    if len(args.args) <= 0 or has_stdin():
        indata = sys.stdin.read()
    kind=""
    if len(args.args) > 0:
        kind=args.args[0]
    header = "```%s" % kind
    code = []
    footer = "```"
    for line in indata.split('\n'):
        total = len(header) + 1
        for x in code:
            total += len(x) + 1
        total += len(footer) + 1
        if total + len(line) + 1 >= 2000:
            for x in [header] + code + [footer]:
                print x
            print '=-=-=-=-=-=-=-=-=-=-=-=-=-='
            code = [line]
        else:
            code += [line]
    if len(code) > 0:
        for x in [header] + code + [footer]:
            print x

def main():
    global args
    if not args:
        args, leftovers = parser.parse_known_args()
        args.args = leftovers
    return run()

if __name__ == "__main__":
    main()

